package com.neu.canteen.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neu.canteen.domain.Customer;
import com.neu.canteen.domain.FoodType;
import com.neu.canteen.service.CustomerService;
import com.neu.canteen.service.impl.CustomerServiceImpl;
import com.neu.canteen.service.impl.FoodTypeServiceImpl;
import com.neu.canteen.utils.JsonModel;
import com.neu.canteen.utils.JsonUtil;
import com.sun.xml.internal.bind.v2.runtime.Name;

public class CustomerController extends HttpServlet{
	
	private CustomerService customerService ;
	public CustomerController()
	{
		customerService=new CustomerServiceImpl();
	}

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException ,IOException {

		List<Customer> customers = customerService.findAll();
		
		JsonModel<List<Customer>> jsonModel = new JsonModel<>();
		jsonModel.setRows(customers);
		jsonModel.setTotal(customers.size());
		
		resp.getWriter().print(JsonUtil.toJSONString(jsonModel));
		
	};
	
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int result=-1;
		int temp = 0;
		String loginName = req.getParameter("loginName");
		String password = req.getParameter("password");
		String action = req.getParameter("action");
		JsonModel jsonModel = new JsonModel();
		Customer customers = new Customer();
		customers = customerService.login(loginName, password);
		if(customers==null){
			jsonModel.setSuccess(false);
			jsonModel.setMsg("账号或密码错误");
			temp=-1;
		}else{
			jsonModel.setSuccess(true);
			jsonModel.setMsg("登录成功");
			jsonModel.setRows(customers);
			result=1;
		}
		
		
		
		
				//TODO 
		if ("add".equals(action)) {
			String name=req.getParameter("name");
			String img=req.getParameter("img");
			String phone=req.getParameter("phone");
			Customer customer = new Customer();

			customer.setLoginName(loginName);
			customer.setName(name);
			customer.setPassword(password);
			customer.setImg(img);
			customer.setPhone(phone);
			result = customerService.add(customer);
		}
		else if ("del".equals(action)) {
			String id = req.getParameter("id");
			int d=Integer.parseInt(id);
			Customer customer=new Customer();
			customer.setId(d);
			result=customerService.deleteCustomoerId(d);
		}else if ("update".equals(action)) {
			String name=req.getParameter("name");
			String img=req.getParameter("img");
			String phone=req.getParameter("phone");
			Customer customer = new Customer();
			
			String id = req.getParameter("id");
			int d=Integer.parseInt(id);
			customer.setId(d);
			
			customer.setLoginName(loginName);
			customer.setName(name);
			customer.setPassword(password);
			customer.setImg(img);
			customer.setPhone(phone);
			result=customerService.updataCustomoerId(customer);
		}
		
		
		if(result==-1){
			if(temp==-1){
				jsonModel.setMsg("账号或密码错误");
				jsonModel.setSuccess(false);
			}
			else {
				jsonModel.setMsg("失败");
				jsonModel.setSuccess(false);
			}
		}else{
			if(temp==0){
				jsonModel.setSuccess(true);
				jsonModel.setMsg("登录成功");
				jsonModel.setRows(customers);
			}else {
				jsonModel.setMsg("成功");
				jsonModel.setSuccess(true);
			}
		}
		
		
		
		resp.getWriter().print(JsonUtil.toJSONString(jsonModel));//{"success":true,"msg":"登录成功"}
	}

}
